﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace WeishaLearningSystemRegester
{
    public class DataConvert
    {
        // Token: 0x0600004C RID: 76 RVA: 0x00003F80 File Offset: 0x00002180
        public static object ChangeType(string value, Type type)
        {
            bool flag = type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
            if (flag)
            {
                bool flag2 = value == null || value.ToString().Length == 0;
                if (flag2)
                {
                    return null;
                }
                NullableConverter nullableConverter = new NullableConverter(type);
                type = nullableConverter.UnderlyingType;
            }
            return DataConvert.ToObject(type, value);
        }

        // Token: 0x0600004D RID: 77 RVA: 0x00003FEC File Offset: 0x000021EC
        public static object ToObject(Type type, string actual)
        {
            object obj = null;
            string name = type.Name;
            string text = name;
            uint num = PrivateImplementationDetails.ComputeStringHash(text);
            if (num <= 2386971688U)
            {
                if (num <= 765439473U)
                {
                    if (num != 423635464U)
                    {
                        if (num != 697196164U)
                        {
                            if (num != 765439473U)
                            {
                                goto IL_36C;
                            }
                            if (!(text == "Int16"))
                            {
                                goto IL_36C;
                            }
                        }
                        else if (!(text == "Int64"))
                        {
                            goto IL_36C;
                        }
                    }
                    else if (!(text == "SByte"))
                    {
                        goto IL_36C;
                    }
                }
                else if (num <= 1324880019U)
                {
                    if (num != 1323747186U)
                    {
                        if (num != 1324880019U)
                        {
                            goto IL_36C;
                        }
                        if (!(text == "UInt64"))
                        {
                            goto IL_36C;
                        }
                    }
                    else if (!(text == "UInt16"))
                    {
                        goto IL_36C;
                    }
                }
                else if (num != 1615808600U)
                {
                    if (num != 2386971688U)
                    {
                        goto IL_36C;
                    }
                    if (!(text == "Double"))
                    {
                        goto IL_36C;
                    }
                }
                else
                {
                    if (!(text == "String"))
                    {
                        goto IL_36C;
                    }
                    return (actual == null) ? "" : actual.Trim();
                }
            }
            else if (num <= 2779444460U)
            {
                if (num != 2615964816U)
                {
                    if (num != 2711245919U)
                    {
                        if (num != 2779444460U)
                        {
                            goto IL_36C;
                        }
                        if (!(text == "Decimal"))
                        {
                            goto IL_36C;
                        }
                    }
                    else if (!(text == "Int32"))
                    {
                        goto IL_36C;
                    }
                }
                else
                {
                    if (!(text == "DateTime"))
                    {
                        goto IL_36C;
                    }
                    bool flag = !string.IsNullOrWhiteSpace(actual);
                    if (flag)
                    {
                        bool flag2 = actual.IndexOf('.') > -1;
                        if (flag2)
                        {
                            actual = actual.Substring(0, actual.LastIndexOf('.'));
                        }
                        Regex regex = new Regex("^\\d+$");
                        bool flag3 = regex.IsMatch(actual.Trim());
                        if (flag3)
                        {
                            long num2 = 0L;
                            long.TryParse(actual, out num2);
                            obj = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds((double)num2);
                        }
                        else
                        {
                            try
                            {
                                obj = DateTime.ParseExact(actual, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
                            }
                            catch
                            {
                                obj = DateTime.ParseExact(actual, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                            }
                        }
                    }
                    return obj;
                }
            }
            else if (num <= 3538687084U)
            {
                if (num != 3409549631U)
                {
                    if (num != 3538687084U)
                    {
                        goto IL_36C;
                    }
                    if (!(text == "UInt32"))
                    {
                        goto IL_36C;
                    }
                }
                else if (!(text == "Byte"))
                {
                    goto IL_36C;
                }
            }
            else if (num != 3969205087U)
            {
                if (num != 4051133705U)
                {
                    goto IL_36C;
                }
                if (!(text == "Single"))
                {
                    goto IL_36C;
                }
            }
            else
            {
                if (!(text == "Boolean"))
                {
                    goto IL_36C;
                }
                bool flag4 = actual == null;
                if (flag4)
                {
                    obj = null;
                }
                else
                {
                    bool flag5 = actual.ToLower() == "true";
                    if (flag5)
                    {
                        obj = true;
                    }
                    else
                    {
                        bool flag6 = actual.ToLower() == "false";
                        if (flag6)
                        {
                            obj = false;
                        }
                        else
                        {
                            obj = null;
                        }
                    }
                }
                return obj;
            }
            bool flag7 = string.IsNullOrWhiteSpace(actual);
            if (flag7)
            {
                actual = "0";
            }
            return Convert.ChangeType(actual, type);
            IL_36C:
            obj = Convert.ChangeType(actual, type);
            return obj;
        }

        // Token: 0x0600004E RID: 78 RVA: 0x00004388 File Offset: 0x00002588
        public static ConvertToAnyValue ChangeType(object value)
        {
            return new ConvertToAnyValue(value);
        }

        // Token: 0x0600004F RID: 79 RVA: 0x000043A0 File Offset: 0x000025A0
        public static DataTable ToDataTable<T>(List<T> list)
        {
            return DataConvert.ToDataTable<T>(list, string.Empty);
        }

        // Token: 0x06000050 RID: 80 RVA: 0x000043C0 File Offset: 0x000025C0
        public static DataTable ToDataTable<T>(List<T> list, string primaryKey)
        {
            bool flag = list.Count < 1;
            DataTable dataTable;
            if (flag)
            {
                dataTable = null;
            }
            else
            {
                T t = list[0];
                Type type = t.GetType();
                PropertyInfo[] properties = type.GetProperties();
                DataTable dataTable2 = new DataTable(typeof(T).Name);
                for (int i = 0; i < properties.Length; i++)
                {
                    dataTable2.Columns.Add(properties[i].Name, properties[i].PropertyType);
                    bool flag2 = properties[i].Name.Equals(primaryKey, StringComparison.CurrentCultureIgnoreCase);
                    if (flag2)
                    {
                        bool flag3 = dataTable2.PrimaryKey == null || dataTable2.PrimaryKey.Length < 1;
                        if (flag3)
                        {
                            dataTable2.PrimaryKey = new DataColumn[] { dataTable2.Columns[properties[i].Name] };
                        }
                    }
                }
                foreach (T t2 in list)
                {
                    object obj = t2;
                    bool flag4 = obj.GetType() != type;
                    if (flag4)
                    {
                        throw new Exception("要转换的集合元素类型不一致");
                    }
                    object[] array = new object[properties.Length];
                    for (int j = 0; j < properties.Length; j++)
                    {
                        array[j] = properties[j].GetValue(obj, null);
                    }
                    dataTable2.Rows.Add(array);
                }
                dataTable = dataTable2;
            }
            return dataTable;
        }

        // Token: 0x06000051 RID: 81 RVA: 0x00004554 File Offset: 0x00002754
        public static string MD5(string val)
        {
            bool flag = string.IsNullOrWhiteSpace(val);
            string text;
            if (flag)
            {
                text = "";
            }
            else
            {
                bool flag2 = string.IsNullOrWhiteSpace(val.ToString());
                if (flag2)
                {
                    text = "";
                }
                else
                {
                    MD5 md = System.Security.Cryptography.MD5.Create();
                    byte[] array = md.ComputeHash(Encoding.UTF8.GetBytes(val.ToString()));
                    string text2 = string.Empty;
                    for (int i = 0; i < array.Length; i++)
                    {
                        text2 += array[i].ToString("x2");
                    }
                    text = text2;
                }
            }
            return text;
        }

        // Token: 0x06000052 RID: 82 RVA: 0x000045EC File Offset: 0x000027EC
        public static string EncryptForDES(string encryptStr, string encryptKey)
        {
            byte[] array = new byte[] { 18, 52, 86, 120, 144, 171, 205, 239 };
            string text;
            try
            {
                bool flag = string.IsNullOrWhiteSpace(encryptKey);
                if (flag)
                {
                    encryptKey = "";
                }
                bool flag2 = encryptKey.Length < 8;
                if (flag2)
                {
                    encryptKey += "12345678";
                }
                bool flag3 = encryptKey.Length > 8;
                if (flag3)
                {
                    encryptKey = encryptKey.Substring(0, 8);
                }
                byte[] bytes = DataConvert.encoding.GetBytes(encryptKey);
                DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
                byte[] bytes2 = DataConvert.encoding.GetBytes(encryptStr);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(bytes, array), CryptoStreamMode.Write);
                cryptoStream.Write(bytes2, 0, bytes2.Length);
                cryptoStream.FlushFinalBlock();
                text = Convert.ToBase64String(memoryStream.ToArray());
            }
            catch
            {
                text = null;
            }
            return text;
        }

        // Token: 0x06000053 RID: 83 RVA: 0x000046CC File Offset: 0x000028CC
        public static string DecryptForDES(string decryptStr, string decryptKey)
        {
            string text;
            try
            {
                byte[] array = new byte[] { 18, 52, 86, 120, 144, 171, 205, 239 };
                byte[] array2 = new byte[decryptStr.Length];
                bool flag = decryptKey.Length > 8;
                if (flag)
                {
                    decryptKey = decryptKey.Substring(0, 8);
                }
                byte[] bytes = DataConvert.encoding.GetBytes(decryptKey);
                DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
                array2 = Convert.FromBase64String(decryptStr);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(bytes, array), CryptoStreamMode.Write);
                cryptoStream.Write(array2, 0, array2.Length);
                cryptoStream.FlushFinalBlock();
                text = DataConvert.encoding.GetString(memoryStream.ToArray());
            }
            catch
            {
                text = null;
            }
            return text;
        }

        // Token: 0x06000054 RID: 84 RVA: 0x0000478C File Offset: 0x0000298C
        public static string EncryptForRSA(string encryptStr, string encryptKey)
        {
            RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(1024, new CspParameters
            {
                Flags = CspProviderFlags.UseMachineKeyStore
            });
            byte[] bytes = DataConvert.encoding.GetBytes(encryptStr);
            byte[] array = rsacryptoServiceProvider.Encrypt(bytes, false);
            return Convert.ToBase64String(array);
        }

        // Token: 0x06000055 RID: 85 RVA: 0x000047D8 File Offset: 0x000029D8
        public static string DecryptForRSA(string decryptStr, string decryptKey)
        {
            bool flag = string.IsNullOrWhiteSpace(decryptStr);
            string text;
            if (flag)
            {
                text = string.Empty;
            }
            else
            {
                RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(1024, new CspParameters
                {
                    Flags = CspProviderFlags.UseMachineKeyStore
                });
                byte[] array = Convert.FromBase64String(decryptStr);
                byte[] array2 = rsacryptoServiceProvider.Decrypt(array, false);
                text = DataConvert.encoding.GetString(array2);
            }
            return text;
        }

        // Token: 0x06000056 RID: 86 RVA: 0x00004838 File Offset: 0x00002A38
        public static string EncryptForBase64(string encryptStr)
        {
            byte[] bytes = DataConvert.encoding.GetBytes(encryptStr);
            return Convert.ToBase64String(bytes);
        }

        // Token: 0x06000057 RID: 87 RVA: 0x0000485C File Offset: 0x00002A5C
        public static string EncryptForBase64UrlEncode(string encryptStr)
        {
            byte[] bytes = DataConvert.encoding.GetBytes(encryptStr);
            string text = Convert.ToBase64String(bytes);
            return HttpUtility.UrlEncode(text);
        }

        // Token: 0x06000058 RID: 88 RVA: 0x00004888 File Offset: 0x00002A88
        public static string DecryptForBase64(string decryptStr)
        {
            byte[] array = Convert.FromBase64String(decryptStr);
            return DataConvert.encoding.GetString(array);
        }

        // Token: 0x04000015 RID: 21
        private static Encoding encoding = Encoding.UTF8;
    }
}
